package com.mac.hosp_pre.order.mq;


import com.mac.hosp_pre.mq.MqConst;
import com.mac.hosp_pre.mq.OrderMqDto;
import com.mac.hosp_pre.order.entity.OrderInfo;
import com.mac.hosp_pre.order.enums.OrderStatusEnum;
import com.mac.hosp_pre.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@RocketMQMessageListener(topic = MqConst.ORDER_TOPIC,consumerGroup = MqConst.ORDER_GROUP, selectorExpression = MqConst.ORDER_TAG_DELAY)
public class OrderConsumer implements RocketMQListener<OrderMqDto> {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onMessage(OrderMqDto orderMqDto) {
      log.info("延迟消费订单消息-请求参数:{}", orderMqDto);
      if (orderMqDto.getOrderId() == null){
          log.error("延迟消费订单消息-订单id为空");
      }else {
          OrderInfo orderInfo = orderService.getById(orderMqDto.getOrderId());
          if (orderInfo == null){
              log.error("延迟消费订单消息-订单不存在,订单ID:{}", orderMqDto.getOrderId());
          }else {
              if (orderInfo.getOrderStatus() == OrderStatusEnum.UNPAID.getStatus().intValue()){
                  //TODO: 发送mq
                  OrderInfo info = new OrderInfo();
                  info.setOrderStatus(OrderStatusEnum.CANCLE.getStatus());
                  info.setId(orderMqDto.getOrderId());
                  orderService.updateById(info);
              }
          }
      }
    }
}
